#include "logcapturedialog.h"
#include "ui_logcapturedialog.h"
#include "qfiledialog.h"
#include "QApplication"


LogCaptureDialog::LogCaptureDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LogCaptureDialog)
{
    ui->setupUi(this);

    ui->pathSelectionToolButton->setText("选择路径");

    ui->savedPathLineEdit->setText(QApplication::applicationDirPath() + "/CapturedLog.dat");

    connect(ui->pathSelectionToolButton, &QToolButton::clicked, this, [&](){
        QFileDialog dialog(this);
        dialog.setFileMode(QFileDialog::AnyFile);
        QString tFileName = dialog.getSaveFileName(this, "保存文件", ui->savedPathLineEdit->text(), QString("dat file(*.dat)"));

        if(tFileName.isEmpty()){
            return;
        }
        ui->savedPathLineEdit->setText(tFileName);

    });

    connect(ui->buttonBox, &QDialogButtonBox::accepted, this, [&](){
        this->mSavedPath = ui->savedPathLineEdit->text();
        emit logCaptureSignal(ui->startTimeEdit->dateTime(), ui->endTimeEdit->dateTime(), ui->savedPathLineEdit->text());
    });
}

LogCaptureDialog::~LogCaptureDialog()
{
    delete ui;
}

bool LogCaptureDialog::setStartDateTime(const QDateTime &datetime)
{
    if (!datetime.isValid()){
        return false;
    }
    ui->startTimeEdit->setDateTime(datetime);
    return true;
}

bool LogCaptureDialog::setEndDateTime(const QDateTime &datetime)
{
    if (!datetime.isValid()){
        return false;
    }
    ui->endTimeEdit->setDateTime(datetime);
    return true;
}
